package second.greedy;

import java.util.Arrays;

public class Solution18 {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (o1, o2) -> {
            if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]);
            else return Integer.compare(o1[1], o2[1]);
        });
        int res = 0;
        int cover = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < cover) {
                res++;
                cover = Math.min(cover, intervals[i][1]);
            } else cover = intervals[i][1];
        }
        return res;
    }
}

